﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace CommonLib.Pattern
{
	public delegate void RegisterEvent(object param);

	public class ListernerSystem
	{
		protected Dictionary<string, RegisterEvent> EventMapper { get; set; }

		public ListernerSystem()
		{
			EventMapper = new Dictionary<string, RegisterEvent>();
		}

		public void Register(string key, RegisterEvent registerEvent)
		{
			if (!EventMapper.ContainsKey(key))
				EventMapper.Add(key, registerEvent);
			else
				Debug.LogError($"添加重复事件 : {key}");
		}

		public void Deregister(string key)
		{
			if (EventMapper.ContainsKey(key))
				EventMapper.Remove(key);
		}

		public void Respon(string key,object param = null)
		{
			if (EventMapper.TryGetValue(key,out RegisterEvent registerEvent))
			{
				registerEvent.Invoke(param);
			}
		}
	}
}